<?php

	class Controller_Albums extends Controller_Base
	{
		function index()
		{
			$this->assignData('albums', WebApp::app()->db->select(array('*'),array('albums')));
		}
		function view()
		{
			$id_albums =  WebApp::app()->db->select(array('id', 'user_id'), array('albums'), "title ='".$this->args[0]."'");
			$user_id_albums = $id_albums[0]->user_id;
			$id_albums = $id_albums[0]->id;
			
			$rez = WebApp::app()->db->select(array('id', 'title', 'type'),array('images'),
			"albums_id = ".$id_albums);
			
			$user_id = WebApp::app()->db->select(array('id'), array('users'), "username = '" . Autorization::getName() . "' or 
																				email = '" . Autorization::getName() . "'"
																				);
			
			$isAuth = ($user_id_albums == $user_id[0]->id) ? true : false;
			
			$this->assignData('images_cat', $rez);
			$this->assignData('isAuth', $isAuth);
			$this->assignData('id_alb', $id_albums);
		}
		
		function preview()
		{
			$admAndMod_id = WebApp::app()->db->select(array('user_id'), array('maskassignment'), "itemname = 'Admin' or itemname = 'Moderator'");
			$user_id = WebApp::app()->db->select(array('id'), array('users'), "username = '" . Autorization::getName() . "' or 
																			   email = '" . Autorization::getName() . "'"
																			   );
			
			$right = false;
			foreach($admAndMod_id as $id)
			{
				if($id->user_id == $user_id[0]->id)
				{
					$right = true;
					break;
				}
			}
			
			if(isset($_POST['ajax']))
			{
				switch($_POST['type'])
				{
					case "add":{
						
						if(empty($_POST['text']))
						{
							echo "false";
							exit;	
						}
						
						$user = WebApp::app()->db->select(array('id', 'display_name'), array('users'), "username = '" . Autorization::getName() . "' or 
																										email = '" . Autorization::getName() . "'");
						
						WebApp::app()->db->insert('comments', array(
																		"title" => $user[0]->display_name,
																		"text" => $_POST['text'],
																		"user_id" => $user[0]->id,
																		"img_id" => $_POST['id_img']
																   ));
					}break;
					case "del":{
						WebApp::app()->db->delete("comments", "id = " . $_POST['id_com'] . " and img_id = " . $_POST['id_img']);
					}break;
					case "sav":{
						if($_POST['text'] != "")
						{
							WebApp::app()->db->update("comments", 
													  array("text" => $_POST['text']), 
													  "id = " . $_POST['id_com'] . " and img_id = " . $_POST['id_img']
													  );	
						}
					}break;
					case "red":{
						$coment = WebApp::app()->db->select(array("*"), array('comments'), "img_id = " . $_POST['id_img'] . " and id = " . $_POST['id_com']);
						echo json_encode($coment[0]);
						exit;
					}break;
				}
				
				$coments = WebApp::app()->db->select(array("*"), array('comments'), "img_id = " . $_POST['id_img']);
				
				array_push($coments, $right);
				array_push($coments, $user_id[0]->id);
						
				echo json_encode($coments);
				exit;
			}
			
			
			$img =  WebApp::app()->db->select(array('id', 'type'), array('images'), "title ='".$this->args[0]."'");
			$coments = WebApp::app()->db->select(array("*"), array('comments'), "img_id = " . $img[0]->id);
			
			$this->assignData('isRight', $right);
			$this->assignData('user_id', $user_id[0]->id);
			$this->assignData('img_id', $img[0]->id);
			$this->assignData('img_type', $img[0]->type);
			$this->assignData('coments', $coments);
			$this->assignData('path', WebApp::app()->getBaseUrl());
		}
		
		function addAlbums()
		{
			if(isset($_POST['ajax']))
			{
				switch($_POST['type'])
				{
					case "add":{
						$alb = WebApp::app()->db->select(array('*'), array('albums'), "title = '" . $_POST['title'] . "'");
						
						//
						if(!empty($alb) || $_POST['idGame'] == NULL || preg_match("/^\w{3,20}$/", $_POST['title']) == false)
						{
							echo "false";
							exit;	
						}
						
						$user_id = WebApp::app()->db->select(array('id'), array('users'), "username = '" . Autorization::getName() . "' or 
																							email = '" . Autorization::getName() . "'");
						
						//echo $_POST['title'];
						//exit;
						
						WebApp::app()->db->insert('albums', array(
																	"title" => $_POST['title'],
																	"descr" => $_POST['dest'],
																	"user_id" => $user_id[0]->id,
																	"game_id" => $_POST['idGame']
																	));
																	
						$game_name = WebApp::app()->db->select(array('*'), array('games'), "id = " . $_POST['idGame']);
						
						//echo json_encode($game_name);
						//echo $game_name
						//exit;
															
						$str = WebApp::app()->getBaseUrl() . "games/view/" . $game_name[0]->name;
						
						echo $str;
						exit;			
					}break;
				}
			}
			
			$games = WebApp::app()->db->select(array('id', 'name'), array('games'));
			
			$this->assignData('games', $games);
			$this->assignData('path', WebApp::app()->getBaseUrl());
		}
		
		function addPreview()
		{
			if(isset($_POST['btnAdd']) &&
			   isset($_POST['title']) &&
			   isset($_POST['dest']) &&
			   isset($_POST['sel_albums']) &&
			   isset($_FILES['img']['name'])
			   )
			{
				$type = strstr($_FILES['img']['name'], '.');
			
				WebApp::app()->db->insert('images', array(
														   'title' => $_POST['title'],
														   'descr' => $_POST['dest'],
														   'type' => $type,
														   'albums_id' => $_POST['sel_albums']
														  ));
														  
				//$idImg = WebApp::app()->db->select(array("max(id) as maxID"),array("images"));
				//$idImg = $idGame[0]->maxID;
				
				$idImg = WebApp::app()->db->select_max("id", "images");
				
				
				if(is_uploaded_file($_FILES['img']['tmp_name']))
				{
					move_uploaded_file($_FILES['img']['tmp_name'], "images/galery/images/". $idImg->maxID . $type);
					
					//Обрезка
					/*copy ("images/galery/images/". $idImg->maxID . $type, 
						  "images/galery/images/mini/". $idImg->maxID . $type
						  );*/
						  
					WebApp::app()->image_resize("images/galery/images/". $idImg->maxID . $type,
												"images/galery/images/mini/". $idImg->maxID . $type,
												156,
												156
												);
				}
				
				//echo $_FILES['img']['tmp_name'];
				//exit;
			}
			
			$user_id = WebApp::app()->db->select(array('id'), array('users'), "username = '" . Autorization::getName() . "' or 
																				email = '" . Autorization::getName() . "'"
																				);
			
			
			$albums = WebApp::app()->db->select(array('id', 'title'), array('albums'), "user_id = " . $user_id[0]->id);
			
			$this->assignData('albums', $albums);
			$this->assignData('path', WebApp::app()->getBaseUrl());
		}
		
		function delAlbums()
		{
			if(isset($_POST['BtnDelete']) && $_POST['sel_albums'] != NULL)
			{
				WebApp::app()->db->delete('albums', "id = " . $_POST['sel_albums']);  
				
				header("Location:" . WebApp::app()->getBaseUrl() . "albums/");
				exit;
			}
			
			$user_id = WebApp::app()->db->select(array('id'), array('users'), "username = '" . Autorization::getName() . "' or 
																				email = '" . Autorization::getName() . "'"
																				);
			
			$albums = WebApp::app()->db->select(array('id', 'title'), array('albums'), "user_id = " . $user_id[0]->id);
			$this->assignData('albums', $albums);
		}
		
		function delPreview()
		{
			if(isset($_POST['ajax']))
			{
				$photo = WebApp::app()->db->select(array('id', 'title'), array('images'), "albums_id = " . $_POST['idAlbum']);
				
				echo json_encode($photo);
				exit;
			}
			
			if(isset($_POST['BtnDelete']) && $_POST['sel_photo'] != NULL && $_POST['sel_albums'] != NULL)
			{
				WebApp::app()->db->delete('images', "id = " . $_POST['sel_photo']);
				
				$albums_name = WebApp::app()->db->select(array("title"), array('albums'), "id = " . $_POST['sel_albums']);  
				
				header("Location:" . WebApp::app()->getBaseUrl() . "albums/view/" . $albums_name[0]->title);
				exit;
			}
			
			$user_id = WebApp::app()->db->select(array('id'), array('users'), "username = '" . Autorization::getName() . "' or 
																				email = '" . Autorization::getName() . "'"
																				);
			
			$albums = WebApp::app()->db->select(array('id', 'title'), array('albums'), "user_id = " . $user_id[0]->id);
			
			$this->assignData('albums', $albums);
			$this->assignData('path', WebApp::app()->getBaseUrl());
		}
		
	}

?>